/*
 * ClientEntryList.cpp
 *
 *  Created on: 28.03.2011
 *      Author: niea
 */

#include "ClientEntryList.hpp"
#include "Lock.hpp"
#include "log.hpp"
#include "Utils.hpp"
#include <algorithm>

namespace efs
{

ClientEntryList::ClientEntryList()
{
	pthread_mutex_init(&m_mutex, NULL);
}

ClientEntryList::~ClientEntryList()
{
	pthread_mutex_destroy(&m_mutex);
}

bool ClientEntryList::addEntry(ClientEntry const& entry)
{
	Lock lock (&m_mutex);
	lock;

	std::vector<ClientEntry>::iterator find_it = std::find(m_entry_list.begin(), m_entry_list.end(), entry);
	if (find_it == m_entry_list.end())
	{
		m_entry_list.push_back(entry);
		pantheios::log_DEBUG("[ClientEntryList]: new entry added");
		entry.print();
		return true;
	}
	return false;
}

void ClientEntryList::removeEntry(ClientEntry const& entry)
{
	Lock lock (&m_mutex);
	lock;

	std::vector<ClientEntry>::iterator find_it = std::find(m_entry_list.begin(), m_entry_list.end(), entry);
	if (find_it != m_entry_list.end())
	{
		pantheios::log_DEBUG("[ClientEntryList]: entry erased");
		find_it->print();
		m_entry_list.erase(find_it);
	}
}

void ClientEntryList::print()
{
	Lock lock (&m_mutex);
	lock;

	pantheios::log_DEBUG("[ClientEntryList]: entry list: [" + intToStr(m_entry_list.size()) + "]");
	std::vector<ClientEntry>::const_iterator it = m_entry_list.begin();
	for(; it != m_entry_list.end(); ++it)
	{
		it->print();
	}
}

std::vector<ClientEntry> ClientEntryList::getList()
{
	Lock lock (&m_mutex);
	lock;

	return m_entry_list;
}

}
